home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / STRREP.C < prev    next >
C/C++ Source or Header  |  1993-04-26  |  2KB  |  73 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville, MI
  3. Date: 04-23-93 (22:26)             Number: 79
  4. From: JERRY COFFIN                 Refer#: NONE
  5.   To: ALL                           Recvd: NO  
  6. Subj: strrep.c                       Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. Recently I posted a routine to find a substring and replace it
  9. with another.  Unluckily, I wrote it, tested it, modified it, and
  10. then posted the first ( incorrect ) version.  With my sincere
  11. apologies, here's a version that works:
  12.  
  13. #include <string.h>
  14.  
  15. /* public domain by Jerry Coffin.  Tested with MS C 7.0
  16.  * I believe this should be portable to just about anything
  17.  * that compiles C.
  18.  */
  19.  
  20. char *str_replace(char *string, char *old, char *new, size_t max_len) {
  21. /* 'max_len' is the maximum number of characters to place in
  22.  * 'string'  If set to anything less than the current size of the
  23.  * string ( eg. 0 ) the string will not be allowed to grow beyond
  24.  * its current length.
  25.  */
  26.  
  27.     char *pos=string;
  28.     size_t len = strlen(string);
  29.     size_t old_len = strlen(old);
  30.     size_t new_len = strlen(new);
  31.     int difference = new_len - old_len;
  32.  
  33.     if (max_len < len )
  34.         max_len = len;
  35.  
  36.     while ( NULL != (pos = strstr(pos,old)) )
  37.         if ( len + difference > max_len )
  38.                 break;
  39.         else {
  40.             if ( 0 != difference )
  41.                 memmove(pos+new_len,pos+old_len,len-old_len+1);
  42.             strncpy(pos,new,new_len);
  43.             len += difference;
  44.             pos+= new_len;
  45.         }
  46.     return string;
  47. }
  48.  
  49. #ifdef TEST
  50. #include <stdio.h>
  51.  
  52. int main(void) {
  53.     char something[100] = "This @bad_name is a bunch of @bad_name";
  54.  
  55.     printf("old string: %s\n",something);
  56.     str_replace(something,"@bad_name","garbage",100);
  57.     printf("new string: %s\n",something);
  58.     str_replace(something," garbage"," garbage.",100);
  59.     printf("changed again: %s\n",something);
  60.     return 0;
  61. }
  62. #endif
  63.  
  64. Again, my apologies for the incorrect version.
  65.         Later,
  66.         Jerry.
  67.  
  68. --- PPoint 1.38
  69.  * Origin: Point Pointedly Pointless (1:128/77.3)
  70. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  71. SEEN-BY: 153/752 154/40 77 157/2 159/100 125 575 950 203/23 209/209 280/1
  72. SEEN-BY: 390/1 396/1 5 15 2270/1 2440/5 3603/20
  73.